local jieee = fk.CreateSkill {
  name = "th_jieee",
}

jieee:addEffect(fk.RoundStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(jieee.name)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local tos = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 2,
      targets = room:getOtherPlayers(player, false),
      skill_name = jieee.name,
      prompt = "#th_jieee-choose"
    })
    if #tos ~= 0 then
      event:setCostData(self, { tos = tos })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self).tos
    if not tos then return end
    room:setPlayerMark(player, "_th_jieee-round", tos)
    for _, to in ipairs(tos) do
      room:setPlayerMark(to, "@@th_jieee-round", 1)
    end
  end
})
jieee:addEffect(fk.TargetConfirming, {
  is_delay_effect = true,
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    local from = data.from
    return player:hasSkill(jieee.name) and from and from ~= player and not from:isNude() and
      table.contains(player:getTableMark("_th_jieee-round"), target) and
      data.card and (data.card.type ~= Card.TypeEquip) and data:isOnlyTarget(target)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    return room:askToSkillInvoke(player, {
      skill_name = jieee.name,
      prompt = "#th_jieee-invoke:"..data.from.id..":"..target.id..":"..data.card:toLogString()
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local from = data.from
    local cid = room:askToChooseCard(player, {
      target = from,
      flag = "he",
      skill_name = jieee.name
    })
    room:throwCard(cid, jieee.name, from, player)
    local card = Fk:getCardById(cid)
    if card.color == Card.Black and from ~= player and from:canUseTo(data.card, player, {bypass_distances = true, bypass_times = true}) then
      data:cancelTarget(target)
      data:addTarget(player)
    elseif card.color == Card.Red then
      if #room:askToDiscard(player, {
        min_num = 1,
        max_num = 1,
        skill_name = jieee.name,
        prompt = "#th_jieee-discard::" .. target.id
      }) == 0 then
        target:drawCards(2, jieee.name)
        room:invalidateSkill(player, jieee.name, "-turn", jieee.name)
      end
    end
  end
})

Fk:loadTranslationTable{
  ["th_jieee"] = "集厄",
  [":th_jieee"] = '每轮开始时，你可以选择至多两名其他角色：本轮其成为其他角色使用非装备牌的唯一目标时，你可以弃置使用者一张牌：<br>' ..
  '若为黑色且你可成为目标，你代替其成为目标；<br>' ..
  "若为红色，你选择一项：⒈弃置一张牌；⒉令其摸两张牌，本回合此技能失效。",

  ["#th_jieee-choose"] = '集厄：你可以选择至多两名其他角色：<br>' ..
  "本轮其成为其他角色使用非装备牌的唯一目标时，你可以弃置使用者一张牌，根据弃置牌不同触发效果",
  ["#th_jieee-invoke"] = "集厄：你可以弃置 %src 一张牌，若为黑色，你代替 %dest 成为 %src 的 %arg 的目标。",
  ["#th_jieee-discard"] = "集厄：你需弃置一张牌，否则 %dest 摸两张牌并令本回合此技能失效。",

  ["@@th_jieee-round"] = "集厄",
}

return jieee